const zh = require('./zh.json')
const en = require('./en.json')
const fs = require('fs')
const path = require('path')
const { includes, isNil } = require('lodash')
const { execSync } = require('child_process')
main()

async function main () {
  // const msg = await execSync('git status', { encoding: 'utf-8' })
  // const disposeLocal = msg.indexOf('Changes not staged for commit:')
  // const disposeStr = disposeLocal !== -1 ? msg.substring(0, disposeLocal) : msg
  // if (!includes(disposeStr, 'zh.json') && !includes(disposeStr, 'en.json')) {
  //   console.log('【i18n:lint】: i18n file not change or commit')
  //   return
  // }
  const newEn = {}
  const tmpEn = {}
  const zhKey = Object.keys(zh)
  zhKey.forEach(z => {
    // 排序，如果不存在时使用空字符串代替
    tmpEn[z] = en[z] ? en[z] : ''
  })
  Object.keys(tmpEn).forEach(e => {
    // 如果en有zh没有的字段，en的字段会被删除
    !isNil(zh[e]) && (newEn[e] = tmpEn[e])
  })
  const newEnString = JSON.stringify(newEn, null, 2)
  fs.writeFileSync(path.resolve('./src/locales/en.json'), newEnString)
  execSync('git add src/locales/en.json')
  console.log('【i18n:lint】: en.json file is update!')
  process.exit(0)
}
